// Created by Joe on 2017/6/15.

angular.module('ins').factory('ins.helper',
    [
        'ins.md',
        function (Middleware) {
            return {
                /**
                 * @type:
                 *  back: stb-back,
                 *  exit: stb-exit&cyber-back
                 *  cyber_exit: cyber-exit
                 *  cyber_menu: cyber-menu
                 */
                platformBack: function (evt, type, test, cb) {
                    if (type === 'back') {
                        test && history.back();
                        cb instanceof Function && cb();
                    } else if (type === 'exit') {
                        if (Middleware.getMiddlewareType() === 'CyberCloud') {
                            cb instanceof Function && cb();
                            history.back();
                            evt.preventDefault();
                        } else {
                            if (test) {
                                console.log('iSTB.browser.gotoSTB(\'tv\')');
                            } else {
                                iSTB.browser.gotoSTB('tv');
                            }
                        }
                    } else if (type === 'cyber_exit') {
                        var url = "file:///uifs/ui1/dvb/fullplay.html";
                        CyberCloud.StartLocalWeb(url, "");
                    } else if (type === 'cyber_menu') {
                        location.href = "../index/index.html";
                    }
                },
                /**
                 * @obj: $scope for example
                 * @name: key
                 * @value: value
                 */
                opStorage: function (obj) {
                    return {
                        set: function (name, value) {
                            value = typeof value === 'object' ? JSON.stringify(value) : value;
                            localStorage.setItem(name, value);
                        },
                        get: function (name, des, isObj) {
                            var value = localStorage.getItem(name);
                            // value = isObj ? JSON.parse(value) : value;
                            obj[des] = value ? (isObj ? JSON.parse(value) : value) : obj[des];
                        },
                        remove: function (caches) {
                            caches.forEach(function (cache) {
                                localStorage.removeItem(cache);
                            });
                        },
                        clear: function () {
                            localStorage.clear();
                        }
                    }
                },
                /**
                 * @obj: $scope for example
                 * @name: key
                 * @value: value
                 * @expire: expire
                 */
                opCache: function (obj) {
                    return {
                        setCookie: function (name, value, expire) {
                            expire = expire || 7;
                            var date = new Date();
                            date.setDate(date.getDate() + expire);
                            var tail = expire === null ? '' : ';expires=' + date.toUTCString();
                            value = typeof value === 'object' ? JSON.stringify(value) : value;
                            document.cookie = name + '=' + escape(value) + tail;
                        },
                        setCookies: function (cookies, expire) {
                            expire = expire || 7;
                            cookies.forEach(function (cookie) {
                                Cookie.setCookie(cookie.name, cookie.value, expire);
                            });
                        },
                        deleteCookie: function (name) {
                            var value = Cookie.getCookie(name);
                            value && Cookie.setCookie(name, '', -1);
                        },
                        deleteCookies: function (cookies) {
                            cookies.forEach(function (cookie) {
                                Cookie.deleteCookie(cookie);
                            });
                        },
                        getCookie: function (name, des, isObj) {
                            var query = name + '=', result = '';
                            if (document.cookie.length === 0) {
                            }

                            var start = document.cookie.indexOf(query);
                            if (start !== -1) {
                                var end = document.cookie.indexOf(';', start + query.length);
                                end === -1 && (end = document.cookie.length);
                                result = unescape(document.cookie.substring(start + query.length, end));
                            }

                            obj[des] = result ? (isObj ? JSON.parse(result) : result) : obj[des];
                        }
                    };
                },
                getQueryStringArgs: function () {
                    // var qs = location.search.length > 0 ? location.search.substring(1) : '';
                    var index = location.href.indexOf('?');
                    var qs = location.href.substring(index + 1);

                    var args = {};
                    var items = qs.length ? qs.split('&') : [];
                    var item = null, name = null, value = null;

                    var len = items.length;
                    for (var i = 0; i < len; i++) {
                        item = items[i].split('=');
                        name = decodeURIComponent(item[0]);
                        value = decodeURIComponent(item[1]);
                        if (name.length) {
                            args[name] = value;
                        }
                    }
                    return args;
                }
            };
        }
    ]);